package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Login;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class PerPasswordActivity extends AppCompatActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.et_Phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_confirm_passwprd)
    EditText etConfirmPasswprd;
    @BindView(R.id.tv_ok)
    Button tvOk;
    @BindView(R.id.et_orpassword)
    EditText etOrpassword;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_per_password);
        ButterKnife.bind(this);

        tvTitle.setText("密码设置");
        dialog = new ProgressDialog(this);
        dialog.setTitle("正在修改密码");
        dialog.setMessage("火速加载中…");
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    Toast.makeText(getApplicationContext(),"报告小主，手机号还没有填呢~",Toast.LENGTH_SHORT).show();
                    etPhone.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(etOrpassword.getText().toString())) {
                    Toast.makeText(getApplicationContext(),"报告小主，原密码还没有填呢~",Toast.LENGTH_SHORT).show();
                    etOrpassword.requestFocus();
                    return;
                }

                if (TextUtils.isEmpty(etPassword.getText().toString())) {
                    Toast.makeText(getApplicationContext(),"报告小主，密码还没有填呢~",Toast.LENGTH_SHORT).show();
                    etPassword.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(etConfirmPasswprd.getText().toString())) {
                    Toast.makeText(getApplicationContext(),"报告小主，确认密码还没有填呢~",Toast.LENGTH_SHORT).show();
                    etConfirmPasswprd.requestFocus();
                    return;
                }

                if (!etPassword.getText().toString().equals(etConfirmPasswprd.getText().toString())) {
                    Toast.makeText(getApplicationContext(),"报告小主，您输入的两次密码不一致哦~~",Toast.LENGTH_SHORT).show();
                    return;

                }
                if (etPassword.getText().toString().equals(etOrpassword.getText().toString())) {
                    Toast.makeText(getApplicationContext(),"报告小主，新密码与原密码一致哦~~",Toast.LENGTH_SHORT).show();
                    return;

                }
                dialog.show();
                OkGo.post(AppConstants.BASE_URL + "/user/add.shtml")
                        .tag(this)
                        .cacheKey("cacheKey")
                        .cacheMode(CacheMode.DEFAULT)
                        .params("orPassWord",AppConstants.md5(etOrpassword.getText().toString()))
                        .params("passWord", AppConstants.md5(etPassword.getText().toString()))
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                removeDialog();
                                Gson gson = new Gson();
                                Login login = gson.fromJson(s, Login.class);

                                if (login.getMsg().equals("success"))
                                {
                                    Toast.makeText(PerPasswordActivity.this, "小主，修改密码成功,请重新登录哦" , Toast.LENGTH_SHORT).show();
                                    Log.e("修改密码",s);
                                    OkGo.post(AppConstants.BASE_URL + "/sys/exit.shtml")
                                            .tag(this)
                                            .cacheKey("cacheKey")
                                            .cacheMode(CacheMode.DEFAULT)
                                            .execute(new StringCallback() {
                                                @Override
                                                public void onSuccess(String s, Call call, Response response) {
                                                    Log.e("退出登录 ",s);




                                                }

                                                @Override
                                                public void onError(Call call, Response response, Exception e) {
                                                    super.onError(call, response, e);
                                                    Toast.makeText(PerPasswordActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                                                }
                                            });

                                    Intent logoutIntent = new Intent(PerPasswordActivity.this, LoginActivity.class);
                                    logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(logoutIntent);
                                    removeDialog();

                                }
                                else {
                                    Toast.makeText(PerPasswordActivity.this, "小主，密码修改失败，请输入正确的手机号或原密码哦" , Toast.LENGTH_SHORT).show();
                                    removeDialog();
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                Toast.makeText(PerPasswordActivity.this, "报告小主，服务器被外星人偷走啦~~" , Toast.LENGTH_SHORT).show();
                                Log.e("密码失败","连接不上服务器");
                            }
                        });




            }
        });

    }
    private void removeDialog(){
        if (dialog!=null&&dialog.isShowing()){
            dialog.dismiss();
        }
    }

}

